package com.lavida.project.companymanager.controller;

import com.lavida.project.system.menu.domain.Menu;
import com.lavida.project.system.menu.mapper.MenuMapper;
import com.lavida.project.system.menu.service.IMenuService;
import com.lavida.project.system.menu.service.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class MenuController1 {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/menu")
    public Map<String, Object> getTree() {
        List<Menu> menus = menuService.selectMenuAll();

        List<Menu> menu1 = new ArrayList<>();
        List<Menu> menu2 = new ArrayList<>();
        List<Menu> menu3 = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();

        for (Menu menu : menus) {
            if (menu.getMenuType() == "M") menu1.add(menu);
            else if (menu.getMenuType() == "C") menu2.add(menu);
            else menu3.add(menu);
        }
        //{id:1,lable:"aaa",children:[]}
        for (Menu menu : menu1) {

            map.put("id", menu.getMenuId().toString());
            map.put("lable", menu.getMenuName());
            map.put("children", new ArrayList<>());

            for (Menu menu4 : menu2) {
                Map<String, Object> map1 = new HashMap<>();
                if (menu4.getParentId() == menu.getMenuId()) {
                    map1.put("id", menu4.getMenuId().toString());
                    map1.put("lable", menu4.getMenuName());
                    map1.put("children", new ArrayList<>());

                    for (Menu menu5 : menu3) {
                        Map<String, Object> map2 = new HashMap<>();
                        if (menu4.getParentId() == menu5.getMenuId()) {
                            map2.put("id", menu5.getMenuId().toString());
                            map2.put("lable", menu5.getMenuName());
                            List children = (List) map1.get("children");
                            children.add(map2);
                        }
                    }
                    List children = (List) map.get("children");
                    children.add(map1);
                }

            }
        }
        return map;
    }
/*
    // 127.0.0.1:8081/Smart-HR/test/?menuIds=1,2,3,4,5,6,7&roleId=1
    @GetMapping("/test")
    @ResponseBody //menuIds:1,2,3,4,5,6,7,
    public String test(Long roleId, List<Long> menuIds) {
        System.out.println(roleId);
        System.out.println(menuIds);

        List<Menu>  menus=   ......;
        List<Long>  roles=   ......;

        List<Object> returnDate=new ArrayList<>();
        // deleteRoleMenuByRoleId(roleId);

        Menu:
                menuId
                muneName;

        {id:menuId,"checked":"true","name":muneName}

        for (Menu menu : menus) {
            if(menu.menuId in roles){

                returnDate.add( {id:menuId,"checked":true,"name":muneName});
            }else{
                returnDate.add( {id:menuId,"checked":false,"name":muneName});
            }
        }


        addRoleMenuByRoleId



        return "s";
    }*/
}
